package ar.Location.UI;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import ar.Location.PlaceObject.Place;
import ar.Location.PlaceObject.PlaceCategory;
import ar.Location.PlaceObject.PlaceCategoryManager;

public class ListPlaceResultAdapter extends BaseAdapter {

	//private static final String TAG = "ListPlaceResultAdapter";
	Context listViewAdapterContext = null;
	ArrayList<Bitmap> listThumbImages = null;

	public ListPlaceResultAdapter(Context context, ArrayList<Bitmap> listImages) {
		// TODO Auto-generated constructor stub
		initContext(context);
		initListImages(listImages);
	}

	private void initListImages(ArrayList<Bitmap> listImages) {
		// TODO Auto-generated method stub
		listThumbImages = listImages;
	}

	private void initContext(Context context) {
		// TODO Auto-generated method stub
		listViewAdapterContext = context;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		PlaceCategory placeCategory = getPlaceCategory();
		if(placeCategory == null) {
			return 0;
		} else {
			return placeCategory.count();
		}
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		PlaceResultItemView placeResultItem = (PlaceResultItemView) convertView;

		if (placeResultItem == null) {
			placeResultItem = new PlaceResultItemView(listViewAdapterContext);
		}

		Place place = getResultAt(position);
		
		Bitmap bmp = listThumbImages.get(position);
		if(place != null) {
			placeResultItem.bindData(place);
			placeResultItem.updateThumbImage(place, bmp);
		}
		
		return placeResultItem;
	}
	
	private PlaceCategory getPlaceCategory() {
		return PlaceCategoryManager.currentPlaceCategory;
	}
	
	private Place getResultAt(int position) {
		// TODO Auto-generated method stub
		PlaceCategory placeCategory = getPlaceCategory();
		if(placeCategory != null && placeCategory.count() > 0) {
			return placeCategory.getPlaceAtIndex(position);
		}
		return null;
	}
}
